//-------------------------------------------------------------------
//	Copyright (c) 2012-2014 Zhirnov Andrey
//	This file is part of the "UXGEN-ES" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

#include "Platforms/Android/ASensorManager.h"
#include "Platforms/Android/APlatform.h"

#if defined( PLATFORM_ANDROID )

namespace Engine
{
namespace Platforms
{

#	define APLATFORM()			AndroidPlatform::GetInstance()
#	define AJAVAINTERFACE()		APLATFORM()->GetJavaInterface()
	
	
/*
=================================================
	Init
=================================================
*/
	bool AndroidSensorManager::Init (EngineSubSystemsRef ess, int supportedSensors)
	{
		Destroy();

		//_subSystems.Override( ess );

		_enabledSensors		= 0;
		_supportedSensors	= supportedSensors;

		_supportedSensors.Set( ESensor::LOCATION );

		return true;
	}
	
/*
=================================================
	Destroy
=================================================
*/
	void AndroidSensorManager::Destroy ()
	{}

/*
=================================================
	EnableSensor
=================================================
*/
	bool AndroidSensorManager::EnableSensor (ESensor::type sensor)
	{
		CHECK_ERR( HasSensor( sensor ) );

		_enabledSensors.Set( sensor, true );
		
		APLATFORM()->Push( ParallelOp::FunctionCall( FunctionBuilder::Create(
			AJAVAINTERFACE(), &AndroidJavaInterface::SetSensorState,
			sensor, true, 0 ) ) );

		return true;
	}
	
/*
=================================================
	DisableSensor
=================================================
*/
	bool AndroidSensorManager::DisableSensor (ESensor::type sensor)
	{
		CHECK_ERR( HasSensor( sensor ) );

		_enabledSensors.Set( sensor, false );
		
		APLATFORM()->Push( ParallelOp::FunctionCall( FunctionBuilder::Create(
			AJAVAINTERFACE(), &AndroidJavaInterface::SetSensorState,
			sensor, false, 0 ) ) );

		return true;
	}
	
/*
=================================================
	SetSensorUpdateRate
=================================================
*/
	bool AndroidSensorManager::SetSensorUpdateRate (ESensor::type sensor, uint rate)
	{
		CHECK_ERR( HasSensor( sensor ) );

		APLATFORM()->Push( ParallelOp::FunctionCall( FunctionBuilder::Create(
			AJAVAINTERFACE(), &AndroidJavaInterface::SetSensorState,
			sensor, IsSensorEnabled( sensor ), rate ) ) );

		return true;
	}
	

#	undef APLATFORM
#	undef AJAVAINTERFACE


}	// Platforms
}	// Engine

#endif	// PLATFORM_ANDROID